package go.mybatis.demo;


import go.mybatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import sun.rmi.server.UnicastServerRef;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by shehuilong on 2018/5/4.
 */
public class MybitisDemo {
    @Test
    public void method() throws IOException {
        SqlSession sqlSession = null;


        try {
            String s = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(s);

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession();

            User user1 = sqlSession.selectOne("UserMapper.queryById", 17l);
            System.out.println(user1);
            System.out.println("================================================");

            List<User> uList = sqlSession.selectList("UserMapper.queryBy");
            for (User user : uList) {
                System.out.println(user);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
